package com.situ.system.performance.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.system.performance.model.PerformanceModel;
import com.situ.system.performance.service.PerformanceService;
import com.situ.system.util.FmtRequest;

/**
 * Servlet implementation class PerformanceServlet
 */
public class PerformanceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	PerformanceService service = new PerformanceService();

	public PerformanceServlet() {
		super();

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		Object res = "";
		switch (action) {
		case "page": // 分页/查询使用
			res = page(request);
			break;
		case "del":
			res = del(request);
			break;
		case "add":
			res = add(request);
			break;
		case "get":
			res = get(request);
			break;
		case "udp":
			res = udp(request);
			break;
		default:
			break;
		}
		FmtRequest.write(response.getWriter(), res);
	}

	public Map<String, Object> page(HttpServletRequest request) {
		PerformanceModel model = new PerformanceModel();
		String e_id = request.getParameter("bianhao");
		model.setE_id(e_id);
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int pageLimit = Integer.parseInt(request.getParameter("pageLimit"));
		model.setPageIndex(pageIndex);
		model.setPageLimit(pageLimit);
		model.setPageOn(true);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", service.selectList(model));
		map.put("count", service.selectCount(model));
		return map;
	}

	public String del(HttpServletRequest request) {
		String id = request.getParameter("id");
		PerformanceModel model = new PerformanceModel();
		model.setE_id(id);
		return service.delete(model);
	}

	public String add(HttpServletRequest request) {
		String e_id = request.getParameter("eid");
		String p_id = request.getParameter("pid");
		String p_score = request.getParameter("score");
		PerformanceModel model = new PerformanceModel(e_id, p_id, p_score);
		return service.insert(model);
	}

	public PerformanceModel get(HttpServletRequest request) {
		String e_id = request.getParameter("code");
		PerformanceModel model = new PerformanceModel(e_id);
		return service.selectModel(model);
	}

	public String udp(HttpServletRequest request) {
		String e_id = request.getParameter("eid");
		String p_score = request.getParameter("score");
		PerformanceModel model = new PerformanceModel(e_id, p_score);
		return service.updata(model);
	}
}
